package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUserCardLogRpcService;
import com.hunttown.mes.rpc.domain.TransportUserCardLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserCardLogDTOQuery;
import com.hunttown.mes.service.TransportUserCardLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportUserCardLogConverts.convert;

/**
 * created by wangjunfu on 2019-07-06 10:59:03
 */
@Service
public class TransportUserCardLogRpcServiceImpl implements TransportUserCardLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUserCardLogRpcServiceImpl.class);

    private final TransportUserCardLogService transportUserCardLogService;

    @Autowired
    public TransportUserCardLogRpcServiceImpl(TransportUserCardLogService transportUserCardLogService) {
        this.transportUserCardLogService = transportUserCardLogService;
    }

    @Override
    public TransportUserCardLogDTO insert(TransportUserCardLogDTO transportUserCardLogDTO) {
        try {
            return convert(transportUserCardLogService.insert(convert(transportUserCardLogDTO)));
        } catch (Exception e) {
            logger.error("TransportUserCardLog create failed.", e);
            throw new RuntimeException("TransportUserCardLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUserCardLogDTO transportUserCardLogDTO) {
        try {
            return transportUserCardLogService.updateInfoById(convert(transportUserCardLogDTO));
        } catch (Exception e) {
            logger.error("TransportUserCardLog update by id failed.", e);
            throw new RuntimeException("TransportUserCardLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUserCardLogDTOQuery transportUserCardLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserCardLogDTOQuery.getW_id() != null && transportUserCardLogDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUserCardLogDTOQuery.getW_id());
            }

            if (transportUserCardLogDTOQuery.getIds() != null && !transportUserCardLogDTOQuery.getIds().equals("")) {
                query.put("ids", transportUserCardLogDTOQuery.getIds());
            }

            if (transportUserCardLogDTOQuery.getW_userId() != null) {
                query.put("w_userId", transportUserCardLogDTOQuery.getW_userId());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getW_orderNo())) {
                query.put("w_orderNo", transportUserCardLogDTOQuery.getW_orderNo());
            }

            if (transportUserCardLogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardLogDTOQuery.getStopFlag());
            }

            if (transportUserCardLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardLogDTOQuery.getDeleteFlag());
            }

            return transportUserCardLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUserCardLog update by query failed.", e);
            throw new RuntimeException("TransportUserCardLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardLogDTO selectById(Integer id) {
        try {
            return convert(transportUserCardLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUserCardLog select by id failed.", e);
            throw new RuntimeException("TransportUserCardLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardLogDTO selectByOrderIdForApi(String orderId) {
        try {
            return convert(transportUserCardLogService.selectByOrderIdForApi(orderId));
        } catch (Exception e) {
            logger.error("TransportUserCardLog select by orderid failed.", e);
            throw new RuntimeException("TransportUserCardLog select by orderid failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardLogDTO selectObjByQuery(TransportUserCardLogDTOQuery transportUserCardLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getOrderId())) {
                query.put("orderId", transportUserCardLogDTOQuery.getOrderId());
            }

            if (transportUserCardLogDTOQuery.getTableId() != null) {
                query.put("tableId", transportUserCardLogDTOQuery.getTableId());
            }

            if (transportUserCardLogDTOQuery.getUserId() != null) {
                query.put("userId", transportUserCardLogDTOQuery.getUserId());
            }

            if (transportUserCardLogDTOQuery.getCardId() != null && transportUserCardLogDTOQuery.getCardId() > 0) {
                query.put("cardId", transportUserCardLogDTOQuery.getCardId());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getCardNo())) {
                query.put("cardNo", transportUserCardLogDTOQuery.getCardNo());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getShowDate())) {
                query.put("showDate", transportUserCardLogDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getStartDate())) {
                query.put("startDate", transportUserCardLogDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getEndDate())) {
                query.put("endDate", transportUserCardLogDTOQuery.getEndDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getYearMonth())) {
                query.put("yearMonth", transportUserCardLogDTOQuery.getYearMonth());
            }

            if (transportUserCardLogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardLogDTOQuery.getStopFlag());
            }

            if (transportUserCardLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardLogDTOQuery.getDeleteFlag());
            }

            return convert(transportUserCardLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUserCardLog select obj by query failed.", e);
            throw new RuntimeException("TransportUserCardLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardLogDTO selectObjWithoutEnt(TransportUserCardLogDTOQuery transportUserCardLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getOrderId())) {
                query.put("orderId", transportUserCardLogDTOQuery.getOrderId());
            }

            if (transportUserCardLogDTOQuery.getTableId() != null) {
                query.put("tableId", transportUserCardLogDTOQuery.getTableId());
            }

            if (transportUserCardLogDTOQuery.getUserId() != null) {
                query.put("userId", transportUserCardLogDTOQuery.getUserId());
            }

            if (transportUserCardLogDTOQuery.getCardId() != null && transportUserCardLogDTOQuery.getCardId() > 0) {
                query.put("cardId", transportUserCardLogDTOQuery.getCardId());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getCardNo())) {
                query.put("cardNo", transportUserCardLogDTOQuery.getCardNo());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getShowDate())) {
                query.put("showDate", transportUserCardLogDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getStartDate())) {
                query.put("startDate", transportUserCardLogDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getEndDate())) {
                query.put("endDate", transportUserCardLogDTOQuery.getEndDate());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getYearMonth())) {
                query.put("yearMonth", transportUserCardLogDTOQuery.getYearMonth());
            }

            if (transportUserCardLogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardLogDTOQuery.getStopFlag());
            }

            if (transportUserCardLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardLogDTOQuery.getDeleteFlag());
            }

            return convert(transportUserCardLogService.selectObjWithoutEnt(query));
        } catch (Exception e) {
            logger.error("TransportUserCardLog select obj without ent failed.", e);
            throw new RuntimeException("TransportUserCardLog select obj without ent failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUserCardLogDTO> getListForPage(TransportUserCardLogDTOQuery transportUserCardLogDTOQuery) {
        Query query = Query.page(transportUserCardLogDTOQuery.getPn(), transportUserCardLogDTOQuery.getPageSize());

        if (transportUserCardLogDTOQuery.getId() != null && transportUserCardLogDTOQuery.getId() > 0) {
            query.put("id", transportUserCardLogDTOQuery.getId());
        }

        if (transportUserCardLogDTOQuery.getIds() != null && !transportUserCardLogDTOQuery.getIds().equals("")) {
            query.put("ids", transportUserCardLogDTOQuery.getIds());
        }

        if (transportUserCardLogDTOQuery.getTableId() != null) {
            query.put("tableId", transportUserCardLogDTOQuery.getTableId());
        }

        if (transportUserCardLogDTOQuery.getRecordId() != null && transportUserCardLogDTOQuery.getRecordId() != -9999) {
            query.put("recordId", transportUserCardLogDTOQuery.getRecordId());
        }

        if (transportUserCardLogDTOQuery.getUserId() != null && transportUserCardLogDTOQuery.getUserId() != -9999) {
            query.put("userId", transportUserCardLogDTOQuery.getUserId());
        }

        if (transportUserCardLogDTOQuery.getCardId() != null && transportUserCardLogDTOQuery.getCardId() > 0) {
            query.put("cardId", transportUserCardLogDTOQuery.getCardId());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getCardNo())) {
            query.put("cardNo", transportUserCardLogDTOQuery.getCardNo());
        }

        if (transportUserCardLogDTOQuery.getRechargeType() != null) {
            query.put("rechargeType", transportUserCardLogDTOQuery.getRechargeType());
        }

        if (transportUserCardLogDTOQuery.getPayStatus() != null) {
            query.put("payStatus", transportUserCardLogDTOQuery.getPayStatus());
        }

        if (transportUserCardLogDTOQuery.getStatus() != null && transportUserCardLogDTOQuery.getStatus() != -9999) {
            query.put("status", transportUserCardLogDTOQuery.getStatus());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getLikeName())) {
            query.put("likeName", transportUserCardLogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getShowDate())) {
            query.put("showDate", transportUserCardLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getStartDate())) {
            query.put("startDate", transportUserCardLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getEndDate())) {
            query.put("endDate", transportUserCardLogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getYearMonth())) {
            query.put("yearMonth", transportUserCardLogDTOQuery.getYearMonth());
        }

        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUserCardLogDTOQuery.getOrderBy());
        }

        if (transportUserCardLogDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportUserCardLogDTOQuery.getStopFlag());
        }

        if (transportUserCardLogDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportUserCardLogDTOQuery.getDeleteFlag());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getDaterange())) {
            String[] date = transportUserCardLogDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportUserCardLogDTO> list = convert(transportUserCardLogService.selectByQuery(query));
        Page<TransportUserCardLogDTO> page = Page.from(transportUserCardLogDTOQuery.getPn(), transportUserCardLogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUserCardLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUserCardLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUserCardLog delete failed.", e);
            throw new RuntimeException("TransportUserCardLog delete failed, Please order your code.", e);
        }
    }

    @Override
    public Float computeTotalAmount(TransportUserCardLogDTOQuery transportUserCardLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserCardLogDTOQuery.getEntId() != null) {
                query.put("entId", transportUserCardLogDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getOrderId())) {
                query.put("orderId", transportUserCardLogDTOQuery.getOrderId());
            }

            if (transportUserCardLogDTOQuery.getTableId() != null) {
                query.put("tableId", transportUserCardLogDTOQuery.getTableId());
            }

            if (transportUserCardLogDTOQuery.getUserId() != null) {
                query.put("userId", transportUserCardLogDTOQuery.getUserId());
            }

            if (transportUserCardLogDTOQuery.getRechargeType() != null) {
                query.put("rechargeType", transportUserCardLogDTOQuery.getRechargeType());
            }

            if (transportUserCardLogDTOQuery.getCardId() != null && transportUserCardLogDTOQuery.getCardId() > 0) {
                query.put("cardId", transportUserCardLogDTOQuery.getCardId());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getCardNo())) {
                query.put("cardNo", transportUserCardLogDTOQuery.getCardNo());
            }

            if (StringUtils.isNotBlank(transportUserCardLogDTOQuery.getShowDate())) {
                query.put("showDate", transportUserCardLogDTOQuery.getShowDate());
            }

            if (transportUserCardLogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardLogDTOQuery.getStopFlag());
            }

            if (transportUserCardLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardLogDTOQuery.getDeleteFlag());
            }

            return transportUserCardLogService.computeTotalAmount(query);

        } catch (Exception e) {
            logger.error("TransportUserCardLog delete failed.", e);
            throw new RuntimeException("TransportUserCardLog compute total amount failed, Please order your code.", e);
        }
    }
}